unit SimGrid2_Unit;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, BitGrid, ExtCtrls, ComCtrls;

type
  TForm1 = class(TForm)
    CheckBox1: TCheckBox;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
    CheckBox2: TCheckBox;
    CheckBox3: TCheckBox;
    CheckBox4: TCheckBox;
    CheckBox5: TCheckBox;
    CheckBox6: TCheckBox;
    CheckBox7: TCheckBox;
    CheckBox8: TCheckBox;
    Button1: TButton;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    StatusBar1: TStatusBar;
    Timer1: TTimer;
    procedure FormCreate(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure CheckBox3Click(Sender: TObject);
    procedure CheckBox4Click(Sender: TObject);
    procedure CheckBox5Click(Sender: TObject);
    procedure CheckBox6Click(Sender: TObject);
    procedure CheckBox7Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure CheckBox8Click(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

const
  LogicStr : Array[Boolean] of String = ('0','1');
var
  Form1: TForm1;
  Grid : TBitGrid;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
  Grid := tBitGrid.Init;
  Grid.xSize := 2;
  Grid.ySize := 2;

  Edit1.Text := '048c159d26ae37bf';
  Edit2.Text := '048c159d26ae37bf';
  Edit3.Text := '048c159d26ae37bf';
  Edit4.Text := '048c159d26ae37bf';
  timer1.Enabled := True;   // don't start until the thing is all set up
end;

procedure TForm1.CheckBox1Click(Sender: TObject);
begin
  Grid.Input[West,0] := TCheckBox(Sender).Checked;
end;

procedure TForm1.CheckBox2Click(Sender: TObject);
begin
  Grid.Input[West,1] := TCheckBox(Sender).Checked;
end;

procedure TForm1.CheckBox3Click(Sender: TObject);
begin
  Grid.Input[South,0] := TCheckBox(Sender).Checked;
end;

procedure TForm1.CheckBox4Click(Sender: TObject);
begin
  Grid.Input[South,1] := TCheckBox(Sender).Checked;
end;

procedure TForm1.CheckBox5Click(Sender: TObject);
begin
  Grid.Input[East,1] := TCheckBox(Sender).Checked;
end;

procedure TForm1.CheckBox6Click(Sender: TObject);
begin
  Grid.Input[East,0] := TCheckBox(Sender).Checked;
end;

procedure TForm1.CheckBox7Click(Sender: TObject);
begin
  Grid.Input[North,1] := TCheckBox(Sender).Checked;
end;

procedure TForm1.CheckBox8Click(Sender: TObject);
begin
  Grid.Input[North,0] := TCheckBox(Sender).Checked;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Grid.AsHex[0,0] := Edit1.Text;   Edit1.Text := Grid.AsHex[0,0];
  Grid.AsHex[1,0] := Edit2.Text;   Edit2.Text := Grid.AsHex[1,0];
  Grid.AsHex[0,1] := Edit3.Text;   Edit3.Text := Grid.AsHex[0,1];
  Grid.AsHex[1,1] := Edit4.Text;   Edit4.Text := Grid.AsHex[1,1];

  Grid.Recalc; 

  Label1.Caption := LogicStr[Grid.Output[West,0]];
  Label2.Caption := LogicStr[Grid.Output[West,1]];
  Label3.Caption := LogicStr[Grid.Output[South,0]];
  Label4.Caption := LogicStr[Grid.Output[South,1]];
  Label5.Caption := LogicStr[Grid.Output[East,1]];
  Label6.Caption := LogicStr[Grid.Output[East,0]];
  Label7.Caption := LogicStr[Grid.Output[North,1]];
  Label8.Caption := LogicStr[Grid.Output[North,0]];
end;

procedure TForm1.Timer1Timer(Sender: TObject);
const
  InputCost  = 0.20;  // picoJoules
  OutputCost = 1.10;  // picoJoules
var
  TotalEnergy : Double;

begin
  Button1.Click;

  TotalEnergy := BitGrid.InputCount * InputCost +
                 BitGrid.OutputCount * OutputCost;
  StatusBar1.Panels[0].Text := Format('Input Transistions: %d',[BitGrid.InputCount]);
  StatusBar1.Panels[1].Text := Format('Output Transitions: %d',[BitGrid.OutputCount]);
  StatusBar1.Panels[2].Text := Format('Energy Used : %f picoJoule',[TotalEnergy]);
end;

end.
